package cn.itcast.bos.web.action.transit;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.service.transit.TransitInfoService;
import cn.itcast.bos.web.action.common.BaseAction;

@SuppressWarnings("serial")
@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class TransitAction extends BaseAction<TransitInfo>{
	
	@Autowired
	private TransitInfoService transitInfoService;
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}
	
	/**
	 * 保存运单中转信息
	 * @return
	 */
	@Action(value="transitInfo_save",results={@Result(name="success",type="json")})
	public String saveTransitInfo(){
		Map<String,Object> map = new HashMap<>();
		try {
			transitInfoService.saveTransitInfo(ids);
			map.put("success", true);
			map.put("msg", "运单开始中转");
		} catch (Exception e) {
			map.put("success", false);
			map.put("msg", "运单中转失败");
		}
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	/**
	 * 中转信息分页显示功能
	 * 因为实体存在集合的引用需要将集合数据转换为 json 返回
	 * 第一种：在业务层手动查询数据 被加载
	 * 第二种：配置立即抓取
	 * 第三种： OpenEntityManagerInView
	 */
	@Action(value="transitInfo_pageQuery",results={@Result(name="success",type="json")})
	public String findPageQuery(){
		Pageable pages = new PageRequest(page - 1, rows);
		Page<TransitInfo> pageData = transitInfoService.findPageQuery(pages);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

}
